#!/usr/bin/env python
# coding=utf-8
'''
对已经获取到的数据进行校验，确定其正确性
'''

from UrlData import getGitUrlData, getRepoName, getSourcePkg, getGitCloneUrl
from cve.DebianPackageUtils import getSourceCodeDirectoryInfo
import os
import os.path as path
from DBUtils import GetDstGT, GetSummaryByCve, GetCveList
from CPE import GetSoftwareByCve
from nlp.EntityExtractor import ExtractEntity
from Tasks import NamedEntityExtract,pattern1 
from celery import group

def __compare_name(lhs, rhs):
    lhs = lhs.lower().rstrip("0123456789")
    rhs = rhs.lower().rstrip("0123456789")
    return lhs == rhs

def isValidCve(cve):
    summary = GetSummaryByCve(cve)
    if summary:
        summary = summary.strip()
    if not summary or summary.startswith("** RESERVED **") or summary.startswith("** REJECT **"):
        return False
    return True

def evaluteGitRepo(pkg):
    '''
    验证下载的git repo的正确性
    通过与Debian Package Source进行目录结构的比较完成
    '''
    url = getGitCloneUrl(pkg)
    repoName = getRepoName(url)
    srcPkg = getSourcePkg(pkg)
    basedir = "/mnt/winkar/git_repos/{}".format(repoName)
    if not path.isdir(basedir):
        return None
    local_directories = []
    for f in os.listdir(basedir):
        # if path.isdir(path.join(basedir, f)):
        local_directories.append(f)
    local_directories.remove(".git") 
    if not local_directories:
        return None
    remote_directories = getSourceCodeDirectoryInfo(srcPkg)
    a = set(local_directories)
    b = set(remote_directories)
    try:
        print "{},{}".format(srcPkg, float(len(a&b))/ float(len(a)))
    except Exception, e:
        import sys
        sys.stderr.write("%s\n" % str(e))
        sys.stderr.write("%s\n" % srcPkg)
            
def evaluteAllGitRepo():
    for _, pkg in getGitUrlData():
        evaluteGitRepo(pkg)

def evaluateExtractor():
    for pkg,cve in GetDstGT():
        softwares = GetSoftwareByCve(cve)
        summary = GetSummaryByCve(cve)
        entities = ExtractEntity(summary)
        print pkg, cve ,softwares, entities
        break

def evaluateExtractResult():
    with open("output/dst_result_all.txt") as f:
        result = f.read().split('\n')
    
    cpe_match_count = 0
    nlp_match_count = 0
    both_match_count = 0
    whole_count = 0

    for l in result:
        if not l.strip():
            continue
        try:
            pkg, cve, cpe, nlp = l.split("::::")
        except:
            print l
            exit()
        if not isValidCve(cve):
            continue
        whole_count += 1
        cpe = eval(cpe)
        nlp = eval(nlp)
        cpe_found = False
        nlp_found = False
        for x in cpe:
            if __compare_name(x, pkg):
                cpe_match_count += 1
                cpe_found = True
                break
        for x in nlp:
            if __compare_name(x, pkg):
                nlp_match_count += 1
                nlp_found = True
                break
        if cpe_found and nlp_found:
            both_match_count += 1
    print "whole_number={}\ncpe_match_count={}\nnlp_match_cout={}\nboth_match_count={}".format(
        whole_count, cpe_match_count, nlp_match_count, both_match_count)
        

def NERPreprocessor():
    async_tasks = []
    for cve, in GetCveList():
        summary = GetSummaryByCve(cve)
        async_tasks.append(NamedEntityExtract.delay(summary))

    read_list = [False] * len(async_tasks)
    while not all(read_list):
        for i,t in enumerate(async_tasks):
            if not read_list[i] and t.ready():
                read_list[i] = True
                if not t.failed():
                    print t.get(propagate=False)
    
def Pattern1Evaluate():
    async_tasks = []
    for cve, in GetCveList():
        summary = GetSummaryByCve(cve)
        async_tasks.append(pattern1.delay(summary))

    read_list = [False] * len(async_tasks)
    while not all(read_list):
        for i,t in enumerate(async_tasks):
            if not read_list[i] and t.ready():
                read_list[i] = True
                if not t.failed():
                    result = t.get(propagate=False)
                    print "TEST_TEXT{}".format(i)
                    print result[0]
                    print result[1]
                elif t.failed():
                    read_list[i] = True




if __name__=="__main__":
    # from sys import argv
    # evaluteGitRepo(argv[1])
    Pattern1Evaluate()
    

